<template>
  <el-dialog :title="title" width="30%" :visible.sync="visible">
    <el-form ref="form" :model="form" class="dialog-form" label-position="right" label-width="120px">
      <el-form-item v-if="type=='pricing'" label="请选择委托方" prop="params.locationId" class="weituof">
        <form-remote-select
          v-model="form.params.locationId"
          :url="$http.conf.api.get('wms').url+'/api/logistics/location/map'"
          :rules="self.page.rules.def"
        />
      </el-form-item>
      <el-form-item label="纸厂单号" prop="params.pageNo">
        <el-input v-model="form.params.pageNo" controls-position="right" />
      </el-form-item>
      <el-form-item label="定价方式">
        <el-radio-group v-model="pricingType">
          <el-radio :label="0">吨</el-radio>
          <el-radio :label="1">趟</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="pricingTitle" prop="params.price" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.price" controls-position="right" :precision="2" :min="1" />
      </el-form-item>
      <el-form-item :label="title" prop="params.actualAmount" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.actualAmount" controls-position="right" :precision="2" :min="1" :disabled="pricingType==1" />
      </el-form-item>
      <el-form-item label="补充描述" prop="remark">
        <el-input v-model="form.params.msg" type="textarea" :rows="2" placeholder="请输入补充描述" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="submit">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  name: 'PurPricing',
  props: {
    value: {
      type: String,
      default: ''
    },
    self: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      visible: false,
      form: {
        message: '',
        url: '',
        params: { actualAmount: 0, price: 0, orderId: '', remark: '', locationId: '', pageNo: '' }
      },
      pricingType: 0,
      pricingTitle: '单价（元/吨）',
      title: '',
      type: ''

    }
  },
  watch: {
    pricingType(val) {
      this.form.params.price = 0
      if (val === 0) {
        this.pricingTitle = '单价（元/吨）'
      } else {
        this.pricingTitle = '单价（元/趟）'
      }
    },
    'form.params.price'(val) {
      if (this.pricingType === 0) {
        this.form.params.actualAmount = Math.floor(this.form.params.price * this.self.form.transportTotal * 100) / 100
      } else {
        this.form.params.actualAmount = this.form.params.price
      }
    }
  },
  created() {
  },
  mounted() {
    this.form.params.orderId = this.value
  },
  methods: {
    show(type, params) {
      this.visible = true
      this.type = type
      if (type === 'pricing') {
        this.title = '总价'
        this.form.url = 'pricing'
      } else if (type === 'accounting') {
        this.title = '核算'
        this.form.url = 'checkComputation'
      }
      this.form.params.pageNo = params.pageNo
    },
    // 提交
    submit() {
      const that = this
      that.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        const loading = that.$loading()
        const url = that.self.page.router.remoteUrl + this.form.url
        that.self.service.post(url, that.form.params).then(res => {
          loading.close()
          if (res.code !== 200) {
            that.$message.error(res.message)
            return
          }
          this.visible = false
          that.$message.success('操作成功!')
          that.self.reload(true)
        }).catch((e) => {
          loading.close()
        })
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.weituof::before{
  content: '*';
  color: red;
  position: relative;
  top: 24px;
  left: 10px;
}
</style>
